/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jsrpc.core;

import java.lang.reflect.Method;
import java.util.Properties;

/**
 *
 * @author neo
 */
public class DefaultClassSupportHandler extends ClassSupportHandler {

    @Override
    public String handle(Properties prop, int index, Method method, Class class1) {
        StringBuilder sm = new StringBuilder();
        sm.append("param_").append(index).append(":{");
        sm.append("javaClass:").append(class1.getName()).append(",");
        sm.append("value:{");
        sm.append("javaClass:");
        sm.append(class1.getName());
        Method[] dmethods = class1.getDeclaredMethods();
        if (dmethods.length > 0) {
            sm.append(",");
        }
        for (int k = 0; k < dmethods.length; k++) {
            Method method1 = dmethods[k];
            String mName = method1.getName();
            if (mName.startsWith("set")) {
                mName = mName.substring(3);
                mName = mName.substring(0, 1).toLowerCase() + mName.substring(1);
                sm.append(mName).append(":");
                Class pClass = method1.getParameterTypes()[0];
                System.out.println("["+ pClass.getName()+".case]");
                String sClass = prop.getProperty(pClass.getName() + ".case");
                if( sClass == null ) {
                    continue;
                }
                int icase = Integer.parseInt(sClass);
                switch (icase) {
                    case 0:
                        sm.append("\\\"\"");
                        sm.append("+param_").append(index).append(".").append(mName).append("+");
                        sm.append("\"\\\"");
                        break;
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        sm.append("\"");
                        sm.append("+param_").append(index).append(".").append(mName).append("+");
                        sm.append("\"");
                        break;
                    case 5:
                        sm.append("\\\"\"");
                        sm.append("+param_").append(index).append(".").append(mName).append("+");
                        sm.append("\"\\\"");
                        break;
                    case 6:
                        sm.append("\\\"\"");
                        sm.append("+param_").append(index).append(".").append(mName).append("+");
                        sm.append("\"\\\"");
                        break;
                    case 7:
                        sm.append("\\\"\"");
                        sm.append("+param_").append(index).append(".").append(mName).append("+");
                        sm.append("\"\\\"");
                        break;     
                }
                if (k < (dmethods.length - 1)) {
                    sm.append(",");
                }
            }
        }
        if( sm.toString().endsWith(",") ) {
            sm.delete(sm.length()-1, sm.length());
        }
        sm.append("}}");
        return sm.toString();
    }
}
